package com.guangbao.dao.impl;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.guanbao.entity.User;
import com.guangbao.dao.UserDao;
import com.guangbao.utils.JdbcUtil;

public class UserDaoImpl implements UserDao {
	
	

	@Override
	public User queryUserByUsername(String username) {
		List<Map<String,Object>> list = JdbcUtil.executeQuery("select * from users where username = ? ", username);
		try {
			if(list.size()>0) {
				User user = new User();
				user.setId(Integer.parseInt(list.get(0).get("id").toString()));
				user.setUsername(list.get(0).get("username").toString());
				user.setPassword(list.get(0).get("password").toString());
				user.setGender(list.get(0).get("gender").toString());
				user.setAge(Integer.parseInt(list.get(0).get("age").toString()));
				user.setBirth(list.get(0).get("birth").toString());
				user.setAddress(list.get(0).get("address").toString());
				return user;
			}
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	@Override
	public User queryUserById(Integer id) {
		
		List<Map<String,Object>> list = JdbcUtil.executeQuery("select * from users where id = ? ", id);
		try {
			if(list.size()>0) {
				User user = new User();
				user.setId(Integer.parseInt(list.get(0).get("id").toString()));
				user.setUsername(list.get(0).get("username").toString());
				user.setPassword(list.get(0).get("password").toString());
				user.setGender(list.get(0).get("gender").toString());
				user.setAge(Integer.parseInt(list.get(0).get("age").toString()));
				user.setBirth(list.get(0).get("birth").toString());
				user.setAddress(list.get(0).get("address").toString());
				return user;
			}
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	@Override
	public int insertUser(User user) {
		String sql = "insert into users(username,password,gender,age,birth,address)values(?,?,?,?,?,?)";
		return JdbcUtil.executeUpdate(sql, user.getUsername(),user.getPassword(),user.getGender(),user.getAge(),user.getBirth(),user.getAddress());
	}

	@Override
	public int updateUser(User user) {
		String sql = "update users set gender=?,age=?,birth=?,address=? where id =?";
		return JdbcUtil.executeUpdate(sql, user.getGender(),user.getAge(),user.getBirth(),user.getAddress(),user.getId());
	}

	@Override
	public List<User> queryAllUser() {
		List<Map<String,Object>> list = JdbcUtil.executeQuery("select * from users");
		try {
			if(list.size()>0) {
				List<User> users = new ArrayList<User>();
				list.stream().forEach((map)->{
					try {
						User user = new User();
						user.setId(Integer.parseInt(map.get("id").toString()));
						user.setUsername(map.get("username").toString());
						user.setPassword(map.get("password").toString());
						user.setGender(map.get("gender").toString());
						user.setAge(Integer.parseInt(map.get("age").toString()));
						user.setBirth(map.get("birth").toString());
						user.setAddress(map.get("address").toString());
						users.add(user);
					}catch(Exception ex) {
						ex.printStackTrace();
					}
				});
				
				return users;
			}
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	@Override
	public int deleteUser(Integer id) {
		return JdbcUtil.executeUpdate("delete from users where id = ?", id);
	}

}
